import First from '@/views/first'

export default{
  validatenull(val) {
    // debugger
    if (val instanceof Array) {
      if (val.length === 0) return true
    } else if (val instanceof Object) {
      if (JSON.stringify(val) === '{}') return true
    } else {
      if (val === 'null' || val == null || val === 'undefined' || val === undefined || val === '') return true
      return false
    }
    return false
  },
  formatRoutes(aMenu){ // 格式化路由
    const aRouter = []
    for(const oMenu of aMenu){
      const {
        path,
        component,
        name,
        icon,
        level,
        id,
        children,
        meta
      } = oMenu

      let componentPath = ''
      if (meta.title !== 'First') {
        componentPath = component
      }

      const oRouter = {
        path: path,
        componentPath: componentPath, // test
        component(resolve) {
          let componentPath = ''
          if (component === 'First') {
            require(['../views/first'], resolve)
            return
          } else {
            componentPath = component
          }
          require([`../${componentPath}`], resolve) // 这里的格式需要特别注意，经测试，'../'这个符号只能写在这里
        },
        level: level,
        id: id,
        name: name,
        meta: meta,
        icon: icon,
        children: this.validatenull(children) ? [] : this.formatRoutes(children)
      }

      console.log('2:',oRouter)
      aRouter.push(oRouter)
    }

    return aRouter
  },
}